package com.huynv.thetopnews;

import java.util.ArrayList;
import java.util.HashMap;
import android.graphics.Typeface;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.text.Html;
import android.view.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import android.content.res.AssetManager;

public class LazyAdapter extends BaseAdapter {
    
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    public int totalNumber=0;
    public SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    //public String currentDateandTime = sdf.format(new Date());
    public Date currentDate = new Date();
    public static String Main_Domain="http://thetopnews.net";
    public Context context;
    public Typeface typefaceTitle,typefaceInfo;
    
    /// Du lieu o day la nhan duoc tu CustomizedListView.java
    public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d,Context context) {
    	//.context=context;
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
        long ts = System.currentTimeMillis();
        //Date localTime = new Date(ts);
        currentDate=new Date(ts);
        //currentDateandTime = sdf.format(new Date(ts));
        typefaceTitle=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf");
        typefaceInfo=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Regular.ttf");
        //Log.e("LazyAdapter", "ok2");
    }
// day se la so luong hien thi len listview
    public int getCount() {
        return data.size();
    }

    // lay 1 item tu vi tri cua no
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    // day la noi gan len listview
    public View getView(int position, View convertView, ViewGroup parent) {
    	totalNumber++;
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
        TextView source = (TextView)vi.findViewById(R.id.source); // source
        TextView date = (TextView)vi.findViewById(R.id.date); // date
        TextView no = (TextView)vi.findViewById(R.id.no); // no
        TextView ranking = (TextView)vi.findViewById(R.id.ranking); // ranking
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        TextView catname = (TextView)vi.findViewById(R.id.catname);
        
        HashMap<String, String> news = new HashMap<String, String>();
        news = data.get(position);
        //AssetManager am=getContext(;
        // Setting all values in listview
        
        title.setTypeface(typefaceTitle);
        source.setTypeface(typefaceInfo);
        date.setTypeface(typefaceInfo);
        catname.setTypeface(typefaceInfo);
        no.setTypeface(typefaceTitle);
        ranking.setTypeface(typefaceTitle);
        title.setText(Html.fromHtml(news.get(MainActivity.KEY_TITLE)));
        if (Config.typeLoad==0){
        	catname.setText(" | "+Utils.getCatName(news.get(MainActivity.KEY_CATID)));
        }else{
        	catname.setText(" | "+news.get(MainActivity.KEY_SOURCE));        
        }
        if (Config.typeLoad==0){
        	source.setText(news.get(MainActivity.KEY_SOURCE)+" | ");
        }else{
        	source.setText("");
        }
        int hourDiff=Integer.parseInt(news.get(MainActivity.KEY_TIMEDIFF));
        String timeDiff="";
        if (hourDiff<=5){
        	timeDiff="5m ago";
        }else{
        	if (hourDiff>=60){
        		hourDiff=hourDiff/60;
        		if (hourDiff==1) {
        			timeDiff="1h ago";
        		}
        		else{
        			//hourDiff=hourDiff/24;
        			if (hourDiff>=24){
        				hourDiff=hourDiff/24;
        				if (hourDiff==1) 
        					timeDiff="1d ago";
        				else
        					timeDiff=hourDiff+"d ago";
        			}else{
        				timeDiff=hourDiff+"h ago";
        			}
        		}
        	} else {
        		timeDiff=hourDiff+"m ago";
        	}
        }
               
        date.setText(timeDiff);
        no.setText(String.valueOf(position+1));
        ranking.setText(news.get(MainActivity.KEY_RANKING));
        //Log.e("getView", String.valueOf(totalNumber));
        String image="";
        try{
        	image=news.get(MainActivity.KEY_IMAGE).trim();
        if (image.isEmpty() || image.equals("") || image.equals(null)) 
        	{
        		//Log.e("getImage", String.valueOf(news.get(MainActivity.KEY_SOURCE)));
        		image=Utils.getDomainImage(news.get(MainActivity.KEY_SOURCE));
        		//Log.e("image=", String.valueOf(image));
        	}
        }catch(Exception ex){
        		image=Utils.getDomainImage(news.get(MainActivity.KEY_SOURCE));
        }
        imageLoader.DisplayImage(image, thumb_image);
        //Log.e("getView,image=", image+":"+String.valueOf(position+1));
        //Log.e("getView", "ok2");
        return vi;
    }
    public int totalHourDiff(String startTime){    	
    	int hours=0;
    	long diff=0;
    	try{
	    	//String ssTime = sdf.parse(startTime).toGMTString();
	    	//String eeTime = sdf.parse(endTime).toGMTString();    		
    		//Log.e("Before parse sTime="," from "+startTime);
	    	Date sTime = sdf.parse(startTime);
	    	sTime = new Date(sTime.getTime() + TimeZone.getDefault().getOffset(currentDate.getTime()));
	    	//Log.e("Before sTime=",sTime.toString()+" from "+startTime);
	    	Date eTime = currentDate;
	    	//Log.e("Before eTime=",eTime.toString());
	    	
	    	//sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
	    	//sTime = Utils.dateParse(sdf.format(sTime));
	    	//Log.e("After sTime=",sTime.toString());
	    	//eTime = Utils.dateParse(sdf.format(eTime));
	    	//Log.e("After eTime=",eTime.toString());
	    	
	    	diff = eTime.getTime() - sTime.getTime();
	    	//Log.e("totalHourDiff:diff=",String.valueOf(diff));
    	}catch(Exception ex){
    		
    	}
    	hours = (int)(diff/(60*60*1000));
    	return Math.abs(hours);
    }
    public int totalMinuteDiff(String startTime){    	
    	int Minute=0;
    	long diff=0;
    	try{
    		//Log.e("Before parse sTime="," from "+startTime);
	    	Date sTime = sdf.parse(startTime);
	    	sTime = new Date(sTime.getTime() + TimeZone.getDefault().getOffset(currentDate.getTime()));
	    	//Log.e("Before sTime=",sTime.toString()+" from "+startTime);
	    	Date eTime = currentDate;
	    	//Log.e("Before eTime=",eTime.toString());
	    	
	    	diff = eTime.getTime() - sTime.getTime();
	    	//Log.e("totalHourDiff:diff=",String.valueOf(diff));
    	}catch(Exception ex){
    		
    	}
    	Minute = (int)(diff/(60*1000));
    	return Math.abs(Minute);
    }
    
}